﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_sub_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
       // this.btnCancel.Attributes.Add("onclick", "return ClearAll()");
        if (!this.IsPostBack) {
            InitItem();
        }
    }
   
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }

    private void AddNew()
    {
        String strMess = "Nick name này đã tồn tại!";
        int flag = -1;
        try
        {
            int ID = -1;
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
            WebData data = new WebData();
            DataLibrary.tbl_TopUrl item = new DataLibrary.tbl_TopUrl();
            item.Url = this.txtUrl.Text.Trim();
            item.TopUrl = txtSubUrl.Text.Trim();
            item.Content_title = txtContent_title.Text;
            item.TopText = txtSubText.Text;
            item.Id = ID;
            flag = data.tbl_TopUrls.UpdateOnSubmit(item);
            strMess = Message.Insert_err;
        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }

        if (flag > 0)
            Response.Redirect(ViewState["URLReferrer"].ToString());
        else
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
    }

    private void InitItem()
    {
        int ID=-1;
        try {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        WebData db=new WebData();
        DataLibrary.tbl_TopUrl item = db.tbl_TopUrls.GetItemById(ID);
        if (item != null) {
            txtUrl.Text = item.Url;
            txtContent_title.Text = item.Content_title;
            txtSubText.Text = item.TopText;
            txtSubUrl.Text = item.TopUrl;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
}